package com.quanroon.ysq.lambda.simple;

import com.google.common.collect.ImmutableMap;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author quanroong.ysq
 * @version 1.0.0
 * @description
 * @createtime 2021/11/30 21:50
 */
public class GeneralFuncation {


    /**
     * @description supplier 生产工厂型
     *
     * 下面给出几个常用的函数式接口：
     *
     * Supplier—get——生产工厂
     * Consumer—accept——消费，即使用
     * Predicate—test——判断
     * Function—apply——类型转换
     */


    /**
     * 生产工厂型
    */
    private static Integer getMax(Supplier<Integer> supplier){

        return supplier.get();
    }

    public static void main(String[] args) {
        int[] arr = {2,34,45,66,22};

        int max = getMax(() ->{
            int maxNum = arr[0];
            for (int i : arr) {
                maxNum = Math.max(maxNum, i);
            }
            return maxNum;
        });
        System.out.println("数组的最大值为："+ max);
    }

    /**
    * @description 消费型
    */
    public static void consume(String s, Consumer<String> consumer){
        consumer.accept(s);
    }

    public static void main1(String[] args) {
        consume("sss", (s) ->{
            System.out.println(s);
        });
    }


    /**
    * 判断型
    */
    public static boolean judge(String s, Predicate<String> predicate){
        return predicate.test(s);
    }

    public static void main2(String[] args){
        boolean ldfdf = judge("Ldfdf", (s) -> s.length() > 5);
    }

    /**
    * 转换类型
    */
    public static int trans(String s, Function<String, Integer> function){
        return function.apply(s);
    }

    public static void main3(String[] args){
        int trans = trans("3434", new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        });
    }

    //定义一个map跟Function组合使用的map
    private final ImmutableMap<Integer, Function<String, String>> PERSON_TYPE_MAP = ImmutableMap.<Integer, Function<String, String>>builder()
            .put(1, person -> String.format("我要这么展示,name:%s", "ss"))
            .put(2, person -> String.format("我要这么展示,name:,age:%s","ss","12"))
            .build();
}
